/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.control.commons;

import java.util.ArrayList;  
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.mags.common.EngineContext;
import org.mags.object.common.ParserFormatterUtils;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityStructure;
import org.mags.remas.model.entity.EntityType;
import org.mags.remas.model.entity.LinkStructure;

/**
 * 
 * @author Mario Gaitan
 *
 */
public final class ControlUtils {
	public static String getParentPath(String path, long entityId) {
		String parentPath=null;
		int index = path.indexOf("/"+entityId+"/");
		if(index>-1)
			parentPath = path.substring(0, index+1);
		return parentPath;
	}
	public static EntityCategory selectCategory(EntityStructure entityStructure) {
		EntityType allowedEntityType;
		Map<EntityCategory, Integer> counters = new HashMap<EntityCategory, Integer>();
		int max = 0;
		for(LinkStructure ls: entityStructure.getLinkStructures()) {
			allowedEntityType = EngineContext.getDataContext().getEntityProviderFactory().getEntityTypeProvider().getById(ls.getId().getLinkEntityTypeId());
			if(counters.get(allowedEntityType.getEntityCategory())==null)
				counters.put(allowedEntityType.getEntityCategory(), 0);
			else {
				if(max<counters.get(allowedEntityType.getEntityCategory())+1)
					max = counters.get(allowedEntityType.getEntityCategory()) + 1;
				counters.put(allowedEntityType.getEntityCategory(), counters.get(allowedEntityType.getEntityCategory())+1);
			}
		}
		for(EntityCategory c: counters.keySet())
			if(counters.get(c)>=max)
				return c;
		return null;
	}
    public static String makeSelectLabel(EntityStructure entityStructure) {
		String text = null;
		EntityType allowedEntityType;
		List<EntityType> entityTypesLoaded = new ArrayList<EntityType>();
		for(LinkStructure ls: entityStructure.getLinkStructures()) {
			allowedEntityType = EngineContext.getDataContext().getEntityProviderFactory().getEntityTypeProvider().getById(ls.getId().getLinkEntityTypeId());
			if(entityTypesLoaded.indexOf(allowedEntityType)<0) {
				if(text==null)
					text = allowedEntityType.getName();
				else
					text = text + ", " + allowedEntityType.getName();
				entityTypesLoaded.add(allowedEntityType);
			}
		}	
		return text;
	}
	@SuppressWarnings("rawtypes")
	public static void setProperty(Object object, String property, Object value) {
    	try {
    		if(value instanceof String) {
    			String _value = (String)value;
	    		Class clazz = PropertyUtils.getPropertyDescriptor(object, property).getClass();
	    		if(clazz==Date.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseDate(_value));
	    		else if(clazz==Float.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseDecimal(_value).floatValue());
	    		else if(clazz==Double.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseDecimal(_value).doubleValue());
	    		else if(clazz==Long.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseInteger(_value).longValue());
	    		else if(clazz==Integer.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseInteger(_value).intValue());
	    		else if(clazz==Short.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseInteger(_value).shortValue());
	    		else if(clazz==Byte.class)
	    			PropertyUtils.setProperty(object, property, ParserFormatterUtils.getInstance().parseInteger(_value).byteValue());
    		}
			BeanUtils.setProperty(object, property, value);
		} catch (Exception e) {
    		if(e instanceof RuntimeException)
    			throw (RuntimeException)e;
    		else
    			throw new RuntimeException(e);
		}  	
    }
    public static Object getProperty(Object object, String property) {
    	try {
    		Object value = PropertyUtils.getProperty(object, property);
    		return value;
    	} catch(Exception e) {
    		if(e instanceof RuntimeException)
    			throw (RuntimeException)e;
    		else
    			throw new RuntimeException(e);
    	}    	
    }
    public static Object getPropertyAsString(Object object, String property) {
    	try {
    		Object value = PropertyUtils.getProperty(object, property);
    		if(value instanceof Date||value instanceof Number)
    			return ParserFormatterUtils.getInstance().object2Text(value);
    		else
    			return value;
    	} catch(Exception e) {
    		if(e instanceof RuntimeException)
    			throw (RuntimeException)e;
    		else
    			throw new RuntimeException(e);
    	}
    }
}
